package cn.edu.lzu.unitweb.controller;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.entity.TotalExamine;
import cn.edu.lzu.entity.User;
import cn.edu.lzu.service.BookRegisteService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Map;

/**
 * @PackageName: cn.edu.lzu.unitweb.controller
 * @ClassName: BookRegisteController
 * @Description: TODO 与预约挂号有关的操作
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/3/9 21:51
 * @Version: 1.0
 */
@Controller
public class BookRegisteController {
    @Resource
    private BookRegisteService bookRegisteService;

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取挂号用户列表
     * @Date: 2020/3/9 20:33
     * @Param:
     * @param null
     * @Return:
     **/
    @ResponseBody
    @RequestMapping("/getRegisteUserList")
    public DataPage getRegisteUserList(int currentPageNum, int pageSize){
        System.out.println("currentPageNum:"+currentPageNum+"\t pageSize:"+pageSize);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        PrincipalCollection principalCollection = (PrincipalCollection) (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY));
        Examiner examiner = (Examiner) principalCollection.getPrimaryPrincipal();
        //单位编号是员工编号的前6位
        long unitId = Long.valueOf((examiner.getUnitId() + "").substring(0, 6));
        //获取一页数据
        DataPage dataPage=bookRegisteService.getRegisteUserList(currentPageNum, pageSize, unitId);
        System.out.println("dataPage.data:\n"+dataPage.getData());
        return dataPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 挂号页面模糊搜索
     * @Date: 2020/3/10 14:55
     * @Param: [searchUserInput, pageSize]
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @ResponseBody
    @RequestMapping("/fuzzySearchRegisteUserById")
    public DataPage fuzzySearchRegisteUserById(String searchUserInput, int pageSize){
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        if(searchUserInput==null || searchUserInput.equals("")){
            DataPage dataPage=bookRegisteService.getRegisteUserList(1, pageSize, examiner.getUnitId());
            return dataPage;
        }
        System.out.println("searchUserInput:"+searchUserInput);
        DataPage dataPage=bookRegisteService.getRegisteUserListBySearchInput(searchUserInput, examiner.getUnitId(), pageSize);
        System.out.println(dataPage);
        return dataPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 缴费页面table
     * @Date: 2020/3/13 22:21
     * @Param: [currentPageNum, pageSize]
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @ResponseBody
    @RequestMapping("/getAllUserChargeMsgList")
    public DataPage getAllUserChargeMsgList(int currentPageNum, int pageSize, int tableType){
        System.out.println("tableType:"+tableType);
        Session session = SecurityUtils.getSubject().getSession();
        PrincipalCollection principalCollection = (PrincipalCollection) (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY));
        Examiner examiner = (Examiner) principalCollection.getPrimaryPrincipal();
        //单位编号是员工编号的前6位
        long unitId = Long.valueOf((examiner.getUnitId() + "").substring(0, 6));
        //获取一页数据
        DataPage dataPage=bookRegisteService.getAllUserChargeMsgList(currentPageNum, pageSize, unitId, tableType);
        System.out.println("allUserChargeMsgAssistListDataPage:"+dataPage);
        return dataPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 缴费页面table模糊搜索
     * @Date: 2020/3/13 22:21
     * @Param: [searchAllUserChargeMsgInput, pageSize, tableType]
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @ResponseBody
    @RequestMapping("/fuzzySearchAllUserChargeMsgList")
    public DataPage fuzzySearchAllUserChargeMsgList(String searchAllUserChargeMsgInput, int pageSize, int tableType){
        System.out.println("tableType:"+tableType);
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        if(searchAllUserChargeMsgInput==null || searchAllUserChargeMsgInput.equals("")){
            DataPage dataPage=bookRegisteService.getAllUserChargeMsgList(1, pageSize, examiner.getUnitId(), tableType);
            return dataPage;
        }
        System.out.println("searchAllUserChargeMsgInput:"+searchAllUserChargeMsgInput);
        DataPage dataPage=bookRegisteService.getAllUserChargeMsgListBySearchInput(searchAllUserChargeMsgInput, examiner.getUnitId(), pageSize, tableType);
        System.out.println(dataPage);
        return dataPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 处理缴费请求
     * @Date: 2020/3/14 22:32
     * @Param: [id]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/handleChargeOperate")
    public ResponseResult handleChargeOperate(int id){
        ResponseResult responseResult=bookRegisteService.handleChargeOperation(id);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO “实验室检查”页面搜索框的数据接口
     * @Date: 2020/3/16 12:13
     * @Param: [currentPageNum, pageSize, searchBookRegisteInput]
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @ResponseBody
    @RequestMapping("/getExamineItemTableMsg")
    public DataPage getExamineItemTableMsg(Integer searchBookRegisteInput, String examineType){
        System.out.println("searchBookRegisteInput:"+searchBookRegisteInput+"\t"+"examineType:"+examineType);
        DataPage dataPage=bookRegisteService.getExamineItemTableMsgDataPage(searchBookRegisteInput, examineType);
        return dataPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取体检编号所属者的信息
     * @Date: 2020/3/16 17:37
     * @Param: []
     * @Return: cn.edu.lzu.entity.User
     **/
    @ResponseBody
    @RequestMapping("/getRegisteUserMsg")
    public Map<String, Object> getRegisteUserMsg(Integer id){
        Map<String, Object> map=bookRegisteService.getRegisteUserMsg(id);
        return map;
    }

    @ResponseBody
    @RequestMapping("/submitTotalExamineResult")
    public ResponseResult submitTotalExamineResult(TotalExamine totalExamine){
        System.out.println("totalExamine = " + totalExamine);
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        ResponseResult responseResult=bookRegisteService.submitTotalExamineResult(totalExamine, examiner);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/getBookedUser")
    public DataPage getBookedUser(Integer currentPageNum,Integer pageSize){
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        DataPage dataPage=bookRegisteService.getBookedUser(currentPageNum, examiner.getUnitId(), pageSize);
        System.out.println(dataPage);
        return dataPage;
    }
}